package com.situ.staffmgr.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.situ.staffmgr.entity.Dept;
import com.situ.staffmgr.service.DeptService;
import com.situ.staffmgr.service.StaffService;
import com.situ.staffmgr.serviceImpl.DeptServiceImpl;
import com.situ.staffmgr.util.BaseServlet;

import sun.management.VMOptionCompositeData;

@WebServlet("/dept.do")
public class DeptServlet extends BaseServlet {
	private DeptService deptService = new DeptServiceImpl();

	public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		int id = Integer.parseInt(request.getParameter("id"));
		String name = request.getParameter("name");
		String room = request.getParameter("room");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		// 封装到一个对象中
		Dept dept = new Dept();
		dept.setId(id);
		dept.setName(name);
		dept.setRoom(room);
		dept.setPhone(phone);
		dept.setEmail(email);
		int result = deptService.edit(dept);
		//3.返回数据 跳转页面
		if (result>0) {
			response.sendRedirect("dept/");
		}else {
			String[] errors = {"ID不存在",//-5
					"电话格式不正确",//-4
					"办公室格式不正确",//-3
					"邮箱格式不正确",//-2
					"部门名字不正确"//-1
					};
			int index=result+5;
			request.setAttribute("error", errors[index]);
			request.getRequestDispatcher("dept/edit.jsp").forward(request, response);
		} 
		
	}

	public void editView(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, IOException, ServletException {
		// 1.获取参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2.业务
		Dept dept = deptService.getById(id);
		// 返回数据跳转页面
		if (dept == null) {
			// 回到列表
			response.sendRedirect("../dept/?error=" + URLEncoder.encode("ID不存在", "UTF-8"));
		} else {
			// 带着数据回到编辑页面
			request.setAttribute("dept", dept);
			request.getRequestDispatcher("dept/edit.jsp").forward(request, response);
		}
	}

	public void remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO Auto-generated method stub
		// 1.获取参数
		int id = Integer.parseInt(request.getParameter("id"));
		// 2.业务逻辑
		int result = deptService.remove(id);
		// 3.返回数据 跳转页面
		if (result > 0) {
			// 删除成功
			response.sendRedirect("dept/");
		} else if (result == -1) {
			// id不存在
			response.sendRedirect("dept/?error=" + URLEncoder.encode("ID不存在", "UTF-8"));// 不能写空格
		} else {
			// 未知错误
			response.sendRedirect("dept/?error=" + URLEncoder.encode("未知错误", "UTF-8"));
		}
	}

	public void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		String room = request.getParameter("room");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		// 封装到一个对象中
		Dept dept = new Dept();
		dept.setName(name);
		dept.setRoom(room);
		dept.setPhone(phone);
		dept.setEmail(email);
		// 业务逻辑
		int result = deptService.add(dept);
		// System.out.println(result);
		switch (result) {
		case 1:
			response.sendRedirect("dept/");
			break;
		case -1:
			request.setAttribute("error", "部门名格式不正常");
			request.getRequestDispatcher("dept/add.jsp").forward(request, response);
		case -2:
			request.setAttribute("error", "邮箱格式不正常");
			request.getRequestDispatcher("dept/add.jsp").forward(request, response);
		case -3:
			request.setAttribute("error", "办公室格式不正常");
			request.getRequestDispatcher("dept/add.jsp").forward(request, response);
		case -4:
			request.setAttribute("error", "电话格式不正常");
			request.getRequestDispatcher("dept/add.jsp").forward(request, response);

		default:
			request.setAttribute("error", "未知错误，请联系管理员");
			request.getRequestDispatcher("dept/add.jsp").forward(request, response);
			break;
		}
	}

	public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		List<Dept> depts = deptService.getAll();
		// 请求转发
		request.setAttribute("depts", depts);
		request.getRequestDispatcher("dept/list.jsp").forward(request, response);
	}
	//获取所有信息，json格式返回
	public void getAll(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
		List<Dept> depts = deptService.getAll();
		//[{"":,"":,...},{},{},...]
		//json是一种键值对的便是方式，比xml更简洁
		//{"name":"tom" ,"age":18,"sex":"男"}
		//使用jackson转换字符串
		ObjectMapper mapper=new ObjectMapper();
		String json=mapper.writeValueAsString(depts);
		//返回数据
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(json);
		
	}


}
